package com.tanhua.server.controller;

import com.tanhua.model.dto.MovementDto;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;

    /**
     * 发布动态
     * @param movementDto
     * @param imageContent
     * @return
     */
    @PostMapping
    public ResponseEntity movements(MovementDto movementDto, MultipartFile imageContent[]) {
        movementService.publishMovement(movementDto, imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询我的动态
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity findByUserId(Long userId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.findByUserId(userId, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询好友动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementService.findFriendMovements(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查询推荐动态列表
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.findRecommendMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询单条动态
     * @param movementId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId) {
        MovementsVo vo = movementService.findById(movementId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 动态点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId){
        Integer count = movementService.addLike(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 取消点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable("id") String movementId){
        Integer count = movementService.disLike(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 动态喜欢
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId){
        Integer count = movementService.addLove(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 取消喜欢
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unLove(@PathVariable("id") String movementId){
        Integer count = movementService.unLove(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 谁看过我
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity queryVisitorsList(){
        List<VisitorsVo> list = movementService.queryVisitorsList();
        return ResponseEntity.ok(list);
    }
}